package com.example.mydemo.controller.hc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.entity.*;
import com.example.mydemo.entity.domain.MenuRoleVO;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.entity.domain.dtree.CheckArr;
import com.example.mydemo.entity.domain.dtree.DTree;
import com.example.mydemo.entity.domain.dtree.DTreeResponse;
import com.example.mydemo.mapper.MenuMapper;
import com.example.mydemo.mapper.RoleMapper;
import com.example.mydemo.model.tableVO;
import com.example.mydemo.service.IMenuRoleService;
import com.example.mydemo.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syf
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService IRoleService;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private IMenuRoleService IMenuRoleService;
    @Autowired
    private MenuMapper menuMapper;
    @PostMapping("/update-role")
    public ResponseEntity<Boolean> updateDbParam(@RequestParam Long id, @RequestParam List<Long> menuList) {
        Boolean added = null;
        System.out.print(menuList);
        int updated = IMenuRoleService.deleteByRoleId(id);
        for(Long i: menuList){
            MenuRole x = new MenuRole();
            x.setMenuId(Math.toIntExact(i));
            x.setRoleId(Math.toIntExact(id));
            added = IMenuRoleService.save(x);
        }
        if (added != null) {
            return ResponseEntity.ok(added);
        }else {
            return ResponseEntity.ok(false);
        }
    }

    @GetMapping("/getRoleById/{id}")
    public DTreeResponse getList(@PathVariable Long id) {
        Role role = roleMapper.selectById(id);
        List<MenuRoleVO> menuRoleVOList = IMenuRoleService.selectByRoleId(id);
        List<Long> menuList= menuRoleVOList.stream().map(MenuRoleVO::getMenuId).collect(Collectors.toList());
        Page<Menu> codepage = new Page<>(1, 1000); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0));
        DTreeResponse a = new DTreeResponse();
        List<Menu> b =sysParamPagePage.getRecords();
        List<DTree> c = new ArrayList<>();
        CheckArr x = new CheckArr("0","0");
        CheckArr y = new CheckArr("0","1");
        for (Menu n : b){
            String muenid,parentId;
            muenid = String.valueOf(n.getMenuId());
            parentId= String.valueOf(n.getParentId());
            List<CheckArr> checkArrs =new ArrayList<>();
            if(menuList.contains(n.getMenuId())){
                checkArrs.add(y);
            }else {
                checkArrs.add(x);
            }
            DTree tree = new DTree(muenid,parentId,n.getTitle(),"-1",null,checkArrs);
            c.add(tree);
        }
        a.setData(c);
            return a;
    }
    @PostMapping("/roleDelete")
    @ResponseBody
    public ResponseDTO<Users> delete(@RequestParam Long id) {
        IRoleService.delete(id);
        return new ResponseDTO<>(200, "删除信息成功！", null);
    }

    @GetMapping("/getRolePage")
    @ResponseBody
    public tableVO getRolePage(@RequestParam Long page, @RequestParam Long limit, @RequestParam(defaultValue = "") String searchParams) {
        Page<Role> codepage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<Role> sysParamPagePage = roleMapper.selectPage(codepage, new UpdateWrapper<Role>().eq("is_delete", 0L));
        tableVO a =new tableVO();
        a.setCode(0L);
        a.setMsg("");
        if (searchParams ==""){
            a.setData(sysParamPagePage.getRecords());
        }else {
            a.setData(IRoleService.selectByName(searchParams));
        }
        a.setCount((long) sysParamPagePage.getTotal());
//        ResponseEntity<List<SysParm>> a =ResponseEntity.ok(b);
        return a;
    }
    @GetMapping("/getRolePage1")
    @ResponseBody
    public tableVO getRolePage1() {
        Page<Role> codepage = new Page<>(1, 1000); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<Role> sysParamPagePage = roleMapper.selectPage(codepage, new UpdateWrapper<Role>().eq("is_delete", 0L));
        tableVO a =new tableVO();
        a.setCode(0L);
        a.setMsg("");
        a.setData(sysParamPagePage.getRecords());
        a.setCount((long) sysParamPagePage.getTotal());
//        ResponseEntity<List<SysParm>> a =ResponseEntity.ok(b);
        return a;
    }
    @PostMapping("/addRole")
    public ResponseDTO<Role> getUsers2(@RequestParam("name") String name , @RequestParam("description") String description) {
        Role user = new Role();
        user.setRoleName(name);
        user.setDescription(description);
        user.setCreateBy("syf");
        IRoleService.saveUser(user);
        return new ResponseDTO<Role>(200, "添加信息成功！", user);
    }
}
